// pages/register/register.js
const api = require("../../config/api.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [{
        name: 1,
        value: '学生',
        checked: 'true'
      },
      {
        name: 2,
        value: '老师'
      },
    ],
    user: {
      username: '',
      password: '',
      classRoom: '',
      status: '1',
      tel: ''
    },
    userInfo:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUserame(even) {
    console.log(even.detail.value)
    this.setData({
      user: {
        username: even.detail.value,
        password: this.data.user.password,
        classRoom: this.data.user.classRoom,
        status: this.data.user.status,
        tel: this.data.user.tel
      }
    })
  },
  getPassword(even) {
    console.log(even.detail.value)
    this.setData({
      user: {
        username: this.data.user.username,
        password: even.detail.value,
        classRoom: this.data.user.classRoom,
        status: this.data.user.status,
        tel: this.data.user.tel
      }
    })
  },
  getclassRoom(even) {
    console.log(even.detail.value)
    this.setData({
      user: {
        username: this.data.user.username,
        password: this.data.user.password,
        classRoom: even.detail.value,
        status: this.data.user.status,
        tel: this.data.user.tel
      }
    })
  },
  radioChange: function (e) {
    this.setData({
      user: {
        username: this.data.user.username,
        password: this.data.user.password,
        status: e.detail.value,
        classRoom: this.data.user.classRoom,
        tel: this.data.user.tel
      },

    })

  },
  getTel: function (even) {
    this.setData({
      user: {
        username: this.data.user.username,
        password: this.data.user.password,
        classRoom: this.data.user.classRoom,
        status: this.data.user.status,
        tel: even.detail.value,
      }
    })
  },
  register() {
    var that = this
    wx.getSetting({
      success(res) {
        console.log(111)
        console.log(res)
        if (!res.authSetting["scope.userInfo"]) {
          wx.authorize({
            scope: 'scope.userInfo',
            success () {
            }
          })
        }
      }
    })
    wx.getUserInfo({
      success: result => {
        console.log(222)
        console.log(result)
        app.globalData.userInfo = result.userInfo
        that.setData({
          userInfo: result.userInfo,
        })
      }
    })
    this.data.user.avatarUrl=app.globalData.userInfo.avatarUrl
    console.log(this.data.user)
    if (this.data.user.username.length == 0 || this.data.user.password.length == 0  || this.data.user.tel.length <11) {
      wx.showToast({
        title: '所有都要认真填哦',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    wx.showModal({
        title: '提示',
        content: '您确定注册吗',
        success(res) {
          if (res.confirm) {
          wx.request({
            url: api.register,
            method: "POST",
            data: that.data.user,
            success: function (res) {

              console.log(res)
              if (res.statusCode == 200) {
                wx.redirectTo({
                  url: '../login/login',
                  success: function () {
                    wx.showToast({
                      title: '注册成功',
                      duration: 2000
                    })
                  }
                })
              } else if (res.statusCode == 202) {
                wx.showToast({
                  title: '账号已经存在',
                  icon: 'none'
                })
              }
            }
          })
        }
      }
      })
    
  }
})